import {createStore} from '@/airstore';
import ApiClient from '@/services/apiClient';
//import {getCurrentGeoLocation} from '@/services/geolocation';
//import {getValue} from '@/services/asyncStorage';

export default createStore({
  namespace: 'NoninstructStore',
  shipParts: [
    /*
    {
      id: 1,
      part_no: '111',
      barcode: 'GJ233',
      batch_no: 'bxx1',
      stock_qty
      qty: 2,
      rack_no: 'rack1',
      rack_shelf_id
      name: '中名',
      special_reason: 'xx',
    },
    */
  ],
  shipOrderNo: '',
  setShipOrderNo(shipOrderNo) {
    this.shipOrderNo = shipOrderNo;
  },
  async getMaterialByRackAndPart(rackTid, rackShelfNo, partno) {
    const srRes = await ApiClient.get('amStore/v1/ScanCode', {
      params: {
        shelfLatticeId: rackTid,
        asdSubName: rackShelfNo,
        apCode: partno,
      },
    });
    return srRes;
  },
  async shipMaterialParts(sooAcNo, iataCode, pickingId, pickerName) {
    /*
    const geolocation = await getCurrentGeoLocation();
    const deviceInfo = await getValue('deviceinfo');
    const reqMeta = {
      // 按后端要求这里重新发送地理位置信息
      ...geolocation,
      brand: deviceInfo.brand,
      system_os: deviceInfo.system_os,
    };
    */
    const smpRes = await ApiClient.post('amStore/v1/doSendOutMoblie', {
      sooAcNo,
      iataCode,
      pickingId,
      sooReceiveName: pickerName,
      amPartMobileAddSubDTO: this.shipParts.map((shp) => ({
        amPartId: shp.id,
        shelfLatticeId: shp.rack_shelf_id,
        snPn: shp.part_no,
        stockNum: shp.stock_qty,
        sellNum: shp.qty,
        chooseReason: shp.special_reason,
      })),
      //reqMeta,
    });
    if (smpRes.errorCode) {
      return smpRes;
    }
    if (
      smpRes.data &&
      smpRes.data.errorInfo &&
      smpRes.data.errorInfo.length > 0
    ) {
      return {
        errorCode: 'ship-error',
        errorMsg: smpRes.data.errorInfo[0],
      };
    }
    this.setShipOrderNo(smpRes.data.orderNum);
    this.clearShipParts();
    return smpRes;
  },
  clearShipParts() {
    this.shipParts = [];
  },
  addShipMaterial(shipMaterial) {
    const existPart = this.shipParts.find(
      (spp) =>
        spp.batch_no === shipMaterial.batch_no &&
        spp.rack_no === shipMaterial.rack_no,
    );
    if (existPart) {
      this.shipParts = this.shipParts.map((spp) => {
        if (spp.id === shipMaterial.id) {
          return {...spp, qty: shipMaterial.qty};
        }
        return spp;
      });
    } else {
      this.shipParts = [shipMaterial, ...this.shipParts];
    }
  },
  adjustShipMaterialQty(shipMaterialId, newQty) {
    if (newQty === 0) {
      this.shipParts = this.shipParts.filter(
        (shp) => shp.id !== shipMaterialId,
      );
    } else {
      this.shipParts = this.shipParts.map((shp) => {
        if (shp.id === shipMaterialId) {
          return {...shp, qty: newQty};
        }
        return shp;
      });
    }
  },
  async getAirportAirplaneRegnos() {
    const arsRes = await ApiClient.get('amStore/v1/listSiteCode');
    const arrRes = await ApiClient.get('amStore/v1/getDropDownAcbiRegno');
    return {
      airports: arsRes.data || [],
      airplaneRegnos: arrRes.data || [],
    };
  },
  async getShipOrderInfo() {
    if (!this.shipOrderNo) {
      return {
        errorCode: 'noid',
        errorMsg: '发料单号为空',
      };
    }
    const soiRes = await ApiClient.get(
      `amStore/v1/getMoblieList/${this.shipOrderNo}`,
    );
    return soiRes;
  },
});
